package com.lee.mysql.controller;

import com.lee.mysql.pojo.TableConfig;
import com.lee.mysql.pojo.TableDbInfo;
import com.lee.mysql.service.TableConfigService;
import com.lee.mysql.service.TableDbInfoService;
import com.lee.mysql.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 数据库信息
 */

@RestController
@RequestMapping("/tableDbInfo")
@Api(tags = {"数据库信息"})
public class TableDbInfoController {
    @Autowired
    private TableDbInfoService tableDbInfoService;
    @Autowired
    private TableConfigService tableConfigService;



    /** 新增数据库信息 */
    @RequiresPermissions("system:dbInfo:add")
    @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
    @ApiOperation(value = "新增数据库信息")
    public Result<String> addRecord(TableDbInfo record){
        TableConfig cfg = tableConfigService.queryOneById(record.getConfigId());

        if ("1".equals(record.getDbType())){
            int i = tableDbInfoService.checkMasterDb(record.getConfigId(),null);
            if (i > 0){
                return new Result<>(Result.FAIL,"只能添加一条源数据库信息",null);
            }
        }

        if ("2".equals(cfg.getConType()) && "2".equals(record.getDbType())){
            int i = tableDbInfoService.checkTargetDb(record.getConfigId());
            if (i > 0){
                return new Result<>(Result.FAIL,"[数据同步类型]仅限添加一条目标数据库信息",null);
            }
        }

        int i = tableDbInfoService.addRecord(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"添加成功",null);
        }else {
            return new Result<>(Result.FAIL,"添加失败",null);
        }
    }

    /** 根据ID修改记录 */
    @RequiresPermissions("system:dbInfo:update")
    @RequestMapping(value = "/updateOneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID修改记录")
    public Result<String> updateOneById(TableDbInfo record){
        if ("1".equals(record.getDbType())){
            int i = tableDbInfoService.checkMasterDb(record.getConfigId(),record.getId());
            if (i > 0){
                return new Result<>(Result.FAIL,"只能有一条源数据库信息",null);
            }
        }

        int i = tableDbInfoService.updateOneById(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"修改成功",null);
        }else {
            return new Result<>(Result.FAIL,"修改失败",null);
        }
    }

    /** 根据ID删除记录 */
    @RequiresPermissions("system:dbInfo:delete")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID删除记录")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    public Result<String> deleteById(String id){
        if (StringUtils.isBlank(id)){
            return new Result<>(Result.FAIL,"请选择要删除的记录",null);
        }

        int i = tableDbInfoService.deleteById(id);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"删除成功",null);
        }else {
            return new Result<>(Result.FAIL,"删除失败",null);
        }
    }


    /** 根据配置ID查询数据库列表 */
    @RequestMapping(value = "/queryByConfigId", method = RequestMethod.POST)
    @ApiOperation(value = "根据配置ID查询数据库列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "configId", value = "配置ID", paramType = "query")
    })
    public Result<List<TableDbInfo>> queryByConfigId(String configId) {

        List<TableDbInfo> list = tableDbInfoService.queryByConfigIdEnCode(configId);

        return new Result<>(Result.SUCCESS,"操作成功",list);
    }




}
